﻿using System.Linq;
using DAL.Model;

namespace BL
{
    public class DocumentSeqNoHelper
    {
        private readonly MasterSession _session;
        private readonly string _docType;

        public DocumentSeqNoHelper (string docType, MasterSession session)
        {
            _session = session;
            _docType = docType;
        }

        public string GetNextDocSeqNo ()
        {
            var docSeq = _session.DocSeqs
                .FirstOrDefault(t => t.DocType == _docType);

            var newDocNo = docSeq != null
                               ? (docSeq.CurrentDocNo += 1).ToString()
                               : "1";
            return newDocNo;
        }

        public void UpdateDocSeqNo (string curentDocNo)
        {
            int newDocNo;
            if (!int.TryParse(curentDocNo, out newDocNo))
            {
                return;
            }
            var docSeq = _session.DocSeqs.FirstOrDefault(t => t.DocType == _docType);
            if (docSeq != null)
            {
                docSeq.CurrentDocNo = newDocNo;
            }
        }
    }
}
